package productlogic

import (
	"context"
	"fmt"
	"strconv"
	"zhongxc.cc/grpc-common/mall-product/types/product"
	"zhongxc.cc/mall-common/errs"
	"zhongxc.cc/mall-product/internal/domain"
	"zhongxc.cc/mall-product/internal/model"
	"zhongxc.cc/mall-product/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type InfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	pmsSkuInfoDomain *domain.PmsSkuInfoDomain
}

func NewInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
	return &InfoLogic{
		ctx:              ctx,
		svcCtx:           svcCtx,
		Logger:           logx.WithContext(ctx),
		pmsSkuInfoDomain: domain.NewPmsSkuInfoDomain(svcCtx.Db),
	}
}

func (l *InfoLogic) Info(in *product.InfoRequest) (*product.InfoResponse, error) {
	// todo: add your logic here and delete this line
	skuId, _ := strconv.ParseInt(in.Id, 10, 64)
	skuInfo, err := l.pmsSkuInfoDomain.FindById(context.Background(), skuId)
	if err != nil {
		return nil, errs.GrpcError(model.ErrorQueryFail)
	}
	if skuInfo != nil {
		return &product.InfoResponse{
			Id:             fmt.Sprintf("%d", skuInfo.SkuId),
			SupName:        skuInfo.SkuName,
			SpuDescription: skuInfo.SkuDesc,
		}, nil
	}
	return &product.InfoResponse{}, nil
}
